﻿using DevExpress.Utils.Win;
using DevExpress.XtraEditors.Popup;
using DevExpress.XtraGrid.Editors;
using DevExpress.XtraLayout;
using DXApplicationTangche.service;
using DXApplicationTangche.UC.面料审核.Service;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace DXApplicationTangche.UC.面料审核
{
    public partial class Form面料审核出库一览 : DevExpress.XtraEditors.XtraForm
    {
        private List<MaterialPlanConfirmIndexDTO> materialPlanConfirmIndexDTOs = new List<MaterialPlanConfirmIndexDTO>();
        private List<ShopRightQueryFacadeDTO> shopRightQueryFacadeDTOs = new List<ShopRightQueryFacadeDTO>();
        private String shopId { get; set; }
        private String shopName { get; set; }

        public Form面料审核出库一览()
        {
            InitializeComponent();
            //this.materialPlanConfirmIndexDTOs = 面料审核Service.getMaterialPlanConfirmIndexAll();
            //this.gridControl面料审核出库一览.DataSource = this.materialPlanConfirmIndexDTOs;
            this.按shopId刷新("18");
            //受托加工方一览
            this.shopRightQueryFacadeDTOs = UserPrivilegesService.getShopRightQueryFacadeByOrgId("shunqiu").shopRightQueryFacadeDTOList;
            this.gridControl受托加工方一览.DataSource = this.shopRightQueryFacadeDTOs;
            this.gridView受托加工方一览.ExpandAllGroups();
            //下拉选择shopId
            this.searchLookUpEdit选择店铺.Properties.DataSource = UserPrivilegesService.getShopRightQueryFacadeByOrgId("shunqiu").shopRightQueryFacadeDTOList;
        }

        private void gridView订单明细_CustomColumnDisplayText(object sender, DevExpress.XtraGrid.Views.Base.CustomColumnDisplayTextEventArgs e)
        {
            if(e.Column.Name.Equals("gridColumn尺码"))
            {
                if (!String.IsNullOrWhiteSpace(e.DisplayText))
                {
                    try { e.DisplayText = e.DisplayText.Replace("IGS_GROUP_SIZE-", "").Replace("EGS_GROUP_SIZE-", ""); } catch { }
                }
            }
        }

        private void 裁剪出库ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            MaterialPlanConfirmIndexDTO materialPlanConfirmIndexDTO裁剪确认 = new MaterialPlanConfirmIndexDTO();
            //try
            //{
                String materialPlanNo = this.bandedGridView面料审核出库一览.GetRowCellValue(this.bandedGridView面料审核出库一览.FocusedRowHandle, "materialPlanNo").ToString();
                foreach(MaterialPlanConfirmIndexDTO materialPlanConfirmIndexDTO in this.materialPlanConfirmIndexDTOs)
                {
                    if (materialPlanNo.Equals(materialPlanConfirmIndexDTO.materialPlanNo))
                    {
                        materialPlanConfirmIndexDTO裁剪确认 = materialPlanConfirmIndexDTO;
                    }
                }
                new Form断料裁剪预览(materialPlanConfirmIndexDTO裁剪确认).ShowDialog();
                this.按shopId刷新(this.shopId);
            //}
            //catch
            //{

            //}            
        }
        private void 刷新()
        {
            this.materialPlanConfirmIndexDTOs = 面料审核Service.getMaterialPlanConfirmIndexAll();
            this.gridControl面料审核出库一览.DataSource = this.materialPlanConfirmIndexDTOs;
            this.bandedGridView面料审核出库一览.RefreshData();
        }
        private void 按shopId刷新(String shopId)
        {
            this.materialPlanConfirmIndexDTOs = 面料审核Service.getMaterialPlanConfirmIndexAllByShopId(shopId);
            this.gridControl面料审核出库一览.DataSource = this.materialPlanConfirmIndexDTOs;
            this.bandedGridView面料审核出库一览.RefreshData();
        }


        private void 制定生产计划ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ShopRightQueryFacadeDTO shopRightQueryFacadeDTO传参 = new ShopRightQueryFacadeDTO();
            ShopInfoDTO shopInfoDTO = new ShopInfoDTO();
            String shopId = this.gridView受托加工方一览.GetRowCellValue(this.gridView受托加工方一览.FocusedRowHandle, "shopId").ToString();
            foreach (ShopRightQueryFacadeDTO shopRightQueryFacadeDTO in this.shopRightQueryFacadeDTOs)
            {
                if (shopId.Equals(shopRightQueryFacadeDTO.shopId))
                {
                    shopInfoDTO = new ShopInfoDTO(shopRightQueryFacadeDTO);
                    shopRightQueryFacadeDTO传参 = shopRightQueryFacadeDTO;
                }
            }
            //获取shopId，传参用
            List<String> shopIds = new List<string>();
            shopIds.Add(shopInfoDTO.shopId);
            //判断返回值是否有值
            List<WIPBatchWIthMBomDTO> wIPBatchWIthMBomDTOs = 面料审核Service.getFabricBOMConsumption(shopIds);
            if (wIPBatchWIthMBomDTOs.Count > 0)
            {
                new Form面料待处理(shopInfoDTO, shopRightQueryFacadeDTO传参).ShowDialog();
            }
            else
            {
                MessageBox.Show("无待处理面料");
                return;
            }
        }
        #region 选择店铺
        private void searchLookUpEdit选择店铺_Popup(object sender, EventArgs e)
        {
            PopupSearchLookUpEditForm form = (sender as IPopupControl).PopupWindow as PopupSearchLookUpEditForm;
            SearchEditLookUpPopup popup = form.Controls.OfType<SearchEditLookUpPopup>().FirstOrDefault();
            LayoutControl layout = popup.Controls.OfType<LayoutControl>().FirstOrDefault();
            //如果窗体内空间没有确认按钮，则自定义确认simplebutton，取消simplebutton，选中结果label
            if (layout.Controls.OfType<Control>().Where(ct => ct.Name == "btOK").FirstOrDefault() == null)
            {
                //得到空的空间
                EmptySpaceItem a = layout.Items.Where(it => it.TypeName == "EmptySpaceItem").FirstOrDefault() as EmptySpaceItem;

                //得到取消按钮，重写点击事件
                Control clearBtn = layout.Controls.OfType<Control>().Where(ct => ct.Name == "btClear").FirstOrDefault();
                LayoutControlItem clearLCI = (LayoutControlItem)layout.GetItemByControl(clearBtn);
                clearBtn.Click += clearBtn_Click;

                //添加一个simplebutton控件(确认按钮)
                LayoutControlItem myLCI = (LayoutControlItem)clearLCI.Owner.CreateLayoutItem(clearLCI.Parent);
                myLCI.TextVisible = false;
            }
        }
        private void searchLookUpEdit选择店铺View_Click(object sender, EventArgs e)
        {
            int[] a = this.searchLookUpEdit选择店铺.Properties.View.GetSelectedRows();
            if (a.Length > 0)
            {
                foreach (int rowHandle in a)
                {
                    this.shopId = this.searchLookUpEdit选择店铺.Properties.View.GetRowCellValue(rowHandle, "shopId").ToString();
                    this.shopName = this.searchLookUpEdit选择店铺.Properties.View.GetRowCellValue(rowHandle, "shopName").ToString();
                }
            }
            this.按shopId刷新(this.shopId);
        }

        /// <summary>
        /// 清除按钮事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void clearBtn_Click(object sender, EventArgs e)
        {
            this.searchLookUpEdit选择店铺.ToolTip = null;
            searchLookUpEdit选择店铺.EditValue = null;
        }
        private void searchLookUpEdit选择店铺_CustomDisplayText(object sender, DevExpress.XtraEditors.Controls.CustomDisplayTextEventArgs e)
        {
            if (null != e.Value)
                e.DisplayText = this.shopName;
        }
        #endregion
    }
}
